package com.example.zqm.git72.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.zqm.git72.Activity.SearchAcitivityActivity;
import com.example.zqm.git72.R;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.adapter.StaticPagerAdapter;

public class HomeFragment extends BaseFragment {
    private TextView home_activity;
    private TextView home_master;
    private TextView home_contest;
    private ImageView search;
    private ActivityFragment activityFragment;
    private MasterFragment masterFragment;
    private ContestFragment contestFragment;
    private View view = null;
    private RollPagerView rollPagerView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate( R.layout.fragment_home, container, false);
        rollPagerView =(RollPagerView)view.findViewById(R.id.home_pic) ;
        //设置播放时间间隔
        rollPagerView.setPlayDelay(1000);
        //设置透明度
       rollPagerView.setAnimationDurtion(500);
        //设置适配器
        rollPagerView.setAdapter(new TestNormalAdapter());
        search=(ImageView)view.findViewById(R.id.search);
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setClass(getActivity(), SearchAcitivityActivity.class);
                startActivity(intent);
            }
        });
        initViews();
        initEvents();
        return view;
    }
    private class TestNormalAdapter extends StaticPagerAdapter {
        private int[] imgs = {
                R.drawable.home1,
                R.drawable.home2,
                R.drawable.home3,
                R.drawable.home4,
        };


        @Override
        public View getView(ViewGroup container, int position) {
            ImageView view = new ImageView(container.getContext());
            view.setImageResource(imgs[position]);
            view.setScaleType(ImageView.ScaleType.CENTER_CROP);
            view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            return view;
        }


        @Override
        public int getCount() {
            return imgs.length;
        }
    }




    @Override
    public void onResume() {
        super.onResume();
        if (activityFragment == null) {
            activityFragment =new ActivityFragment();
            addFragment(activityFragment);
        } else {
            replaceFragment(activityFragment);

        }

    }

    public void initViews(){
        home_activity = (TextView)view.findViewById(R.id.home_activity);
        home_master = (TextView)view.findViewById(R.id.home_master);
        home_contest = (TextView)view.findViewById(R.id.home_contest);
        home_activity.setSelected(true);
        home_activity.setTextColor(getResources().getColor(R.color.cardview_dark_background));
        home_master.setTextColor(getResources().getColor(R.color.cardview_shadow_start_color));
        home_contest.setTextColor(getResources().getColor(R.color.cardview_shadow_start_color));
    }
    public void initEvents(){
        home_activity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(activityFragment == null){
                    activityFragment = new ActivityFragment();
                }
                replaceFragment(activityFragment);
                home_activity.setSelected(true);
                home_master.setSelected(false);
                home_contest.setSelected(false);
                home_activity.setTextColor(getResources().getColor(R.color.cardview_dark_background));
                home_master.setTextColor(getResources().getColor(R.color.cardview_shadow_start_color));
                home_contest.setTextColor(getResources().getColor(R.color.cardview_shadow_start_color));
            }
        });

        home_master.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if( masterFragment== null){
                    masterFragment = new MasterFragment();
                }
                replaceFragment(masterFragment);
                home_master.setSelected(true);
                home_activity.setSelected(false);
                home_contest.setSelected(false);
                home_master.setTextColor(getResources().getColor(R.color.cardview_dark_background));
                home_activity.setTextColor(getResources().getColor(R.color.cardview_shadow_start_color));
                home_contest.setTextColor(getResources().getColor(R.color.cardview_shadow_start_color));
            }
        });
        home_contest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if( contestFragment== null){
                    contestFragment = new ContestFragment();
                }
                replaceFragment(contestFragment);
                home_contest.setSelected(true);
                home_activity.setSelected(false);
                home_master.setSelected(false);
                home_contest.setTextColor(getResources().getColor(R.color.cardview_dark_background));
                home_activity.setTextColor(getResources().getColor(R.color.cardview_shadow_start_color));
                home_master.setTextColor(getResources().getColor(R.color.cardview_shadow_start_color));
            }
        });
    }

}
